﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using NK.Mvc3.DBL;
using NK.Mvc3.Models;

namespace NK.Mvc3.Business
{
    public class StoreIO
    {
        private readonly StoreContext _db;
        public StoreContext DB
        {
            get { return _db; }
        }
        public StoreIO(StoreContext db)
        {
            _db = db;
        }

        #region StoreInput
        public void AddStoreInput(StoreInput storeInput)
        {
            
            if (storeInput != null && storeInput.StoreInputDetails != null)
            {
                foreach (var detail in storeInput.StoreInputDetails)
                {
                    ProductInfo productInfo = DB.ProductInfos.Find(detail.ProductInfoID);
                    productInfo.AddStoreInputQuantity(detail.Quantity);
                    DB.Entry(productInfo).State= EntityState.Modified;
                }
            }
            DB.StoreInputs.Add(storeInput);
        }

        public void RemoveStoreInput(StoreInput storeInput)
        {
            if (storeInput != null && storeInput.StoreInputDetails != null)
            {
                foreach (var detail in storeInput.StoreInputDetails)
                {
                    ProductInfo productInfo = DB.ProductInfos.Find(detail.ProductInfoID);
                    productInfo.MinusStoreInputQuantity(detail.Quantity);
                    DB.Entry(productInfo).State = EntityState.Modified;
                }
            }
            DB.StoreInputs.Remove(storeInput);

        }
        #endregion

        #region StoreInputDetail
        public void AddStoreInputDetail(StoreInputDetail storeInputDetail)
        {
            StoreInput storeInput = DB.StoreInputs.Find(storeInputDetail.StoreInputID);
            if (storeInput != null)
            {
                storeInput.StoreInputDetails.Add(storeInputDetail);
                ProductInfo productInfo = DB.ProductInfos.Find(storeInputDetail.ProductInfoID);
                productInfo.AddStoreInputQuantity(storeInputDetail.Quantity);
                DB.Entry(productInfo).State = EntityState.Modified;
            }
        }

        public void RemoveStoreInputDetail(StoreInputDetail storeInputDetail)
        {
             StoreInput storeInput = DB.StoreInputs.Find(storeInputDetail.StoreInputID);
             if (storeInput != null)
             {
                 storeInput.StoreInputDetails.Remove(storeInputDetail);

                 ProductInfo productInfo = DB.ProductInfos.Find(storeInputDetail.ProductInfoID);
                 productInfo.MinusStoreInputQuantity(storeInputDetail.Quantity);
                 DB.Entry(productInfo).State = EntityState.Modified;
             }
        }

        public void UpdateStoreInputDetail(StoreInputDetail storeInputDetail)
        {
            var oldObj = DB.StoreInputDetails.Find(storeInputDetail.ID);
            var productInfo = DB.ProductInfos.Find(storeInputDetail.ProductInfoID);
            productInfo.AddStoreInputQuantity(storeInputDetail.Quantity);
            productInfo.MinusStoreInputQuantity(oldObj.Quantity);
            DB.Entry(storeInputDetail).State = EntityState.Modified;
        }
        #endregion

        #region StoreOutput
        public void AddStoreOutput(StoreOutput storeOutput)
        {
            if (storeOutput != null && storeOutput.StoreOutputDetails != null)
            {
                foreach (var detail in storeOutput.StoreOutputDetails)
                {
                    detail.ProductInfo.AddStoreOutputQuantity(detail.Quantity);
                    DB.Entry(detail.ProductInfo).State = EntityState.Modified;
                }
            }

            DB.StoreOutputs.Add(storeOutput);
        }

        public void RemoveStoreOutput(StoreOutput storeOutput)
        {
            if (storeOutput != null && storeOutput.StoreOutputDetails != null)
            {
                foreach (var detail in storeOutput.StoreOutputDetails)
                {
                    detail.ProductInfo.MinusStoreOutputQuantity(detail.Quantity);
                    DB.Entry(detail.ProductInfo).State = EntityState.Modified;
                }
            }

            DB.StoreOutputs.Remove(storeOutput);
        }
        #endregion

        #region StoreOutputDetail
        public void AddStoreOutputDetail(StoreOutputDetail storeOutputDetail)
        {
            StoreOutput storeOutput = DB.StoreOutputs.Find(storeOutputDetail.StoreOutputID);
            if (storeOutput != null)
            {
                storeOutput.StoreOutputDetails.Add(storeOutputDetail);

                ProductInfo productInfo = DB.ProductInfos.Find(storeOutputDetail.ProductInfoID);
                productInfo.AddStoreOutputQuantity(storeOutputDetail.Quantity);
                DB.Entry(productInfo).State = EntityState.Modified;
            }
        }

        public void RemoveStoreOutputDetail(StoreOutputDetail storeOutputDetail)
        {
            StoreOutput storeOutput = DB.StoreOutputs.Find(storeOutputDetail.StoreOutputID);
            if (storeOutput != null)
            {
                storeOutput.StoreOutputDetails.Remove(storeOutputDetail);
                ProductInfo productInfo = DB.ProductInfos.Find(storeOutputDetail.ProductInfoID);
                productInfo.MinusStoreOutputQuantity(storeOutputDetail.Quantity);
                DB.Entry(productInfo).State = EntityState.Modified;
            }
        }

        public void UpdateStoreOutputDetail(StoreOutputDetail storeOutputDetail)
        {
            var oldObj = DB.StoreOutputDetails.Find(storeOutputDetail.ID);
            var productInfo = DB.ProductInfos.Find(storeOutputDetail.ProductInfoID);
            productInfo.MinusStoreOutputQuantity(oldObj.Quantity);
            productInfo.AddStoreOutputQuantity(storeOutputDetail.Quantity);
            DB.Entry(storeOutputDetail).State = EntityState.Modified;
        }
        #endregion

        public void Save()
        {
            DB.SaveChanges();
        }

      
    }
}